package com.example.nccdeploytool.util;

import com.example.nccdeploytool.entity.Config;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConfigLoader {

    public static ObjectNode loadConfig() {
        Yaml yaml = new Yaml();
        ObjectMapper jsonMapper = new ObjectMapper();
        try (InputStream in = ConfigLoader.class
                .getClassLoader()
                .getResourceAsStream("config.yml")) {
            Map<String, Object> yamlMap = yaml.load(in);
            ObjectNode objectNode = jsonMapper.valueToTree(yamlMap);
//            String s = jsonMapper.writerWithDefaultPrettyPrinter().writeValueAsString(objectNode);
            return objectNode;
        } catch (IOException e) {
            throw new RuntimeException("Error reading or parsing YAML file", e);
        }
    }

    public static ObjectNode loadConfigJson() {
        try (InputStream in = ConfigLoader.class
                .getClassLoader()
                .getResourceAsStream("config.json")) {

            byte[] bytes = in.readAllBytes();
            String configJsonStr = new String(bytes, "UTF-8");
            ObjectNode objectNode = new ObjectMapper().readValue(configJsonStr, ObjectNode.class);
            return objectNode;
        } catch (IOException e) {
            throw new RuntimeException("Error reading or parsing YAML file", e);
        }
    }

    public static List<Map<String,Object>> loadJsonArr() {
        try (InputStream in = ConfigLoader.class
                .getClassLoader()
                .getResourceAsStream("config.json")) {

            byte[] bytes = in.readAllBytes();
            String configJsonStr = new String(bytes, "UTF-8");
            List<Map<String,Object>> objectNode = new ObjectMapper().readValue(
                    configJsonStr
                    , new TypeReference<List<Map<String,Object>>>() {}
            );
            return objectNode;
        } catch (IOException e) {
            throw new RuntimeException("Error reading or parsing YAML file", e);
        }
    }

    public static List<Map<String,Object>> loadJsonArr(String tableName) {
        try (InputStream in = ConfigLoader.class
                .getClassLoader()
                .getResourceAsStream("db/"+tableName)) {

            byte[] bytes = in.readAllBytes();
            String configJsonStr = new String(bytes, "UTF-8");
            List<Map<String,Object>> objectNode = new ObjectMapper().readValue(
                    configJsonStr
                    , new TypeReference<List<Map<String,Object>>>() {}
            );
            return objectNode;
        } catch (IOException e) {
            throw new RuntimeException("Error reading or parsing YAML file", e);
        }
    }


    public static void writeJsonArr(List<Map<String,Object>> configs) throws Exception {
        String jsonStr = new ObjectMapper().writeValueAsString(configs);

        String path = ConfigLoader.class
                .getClassLoader()
                .getResource("config.json").getPath();


        Files.write(Paths.get(path), jsonStr.getBytes(StandardCharsets.UTF_8));
    }


    public static void main(String[] args) {
//        JSON jsonObject = ConfigLoader.loadConfig();
//        System.out.println(jsonObject);
    }

}